import 'dart:convert';

class PostList {
    final String id;
    final String title;
    final String summary;
    final String coverUrl;
    final int count;
    final int likeCount;
    final int isRecommend;
    final String status;
    final bool isCommentOpen;
    final bool isTop;
    final dynamic publishTime;
    final DateTime createTime;
    final DateTime updateTime;
    final Category category;
    final List<Category> tags;
    final Author author;

    PostList({
        required this.id,
        required this.title,
        required this.summary,
        required this.coverUrl,
        required this.count,
        required this.likeCount,
        required this.isRecommend,
        required this.status,
        required this.isCommentOpen,
        required this.isTop,
        required this.publishTime,
        required this.createTime,
        required this.updateTime,
        required this.category,
        required this.tags,
        required this.author,
    });

    factory PostList.fromRawJson(String str) => PostList.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory PostList.fromJson(Map<String, dynamic> json) => PostList(
        id: json["id"],
        title: json["title"],
        summary: json["summary"],
        coverUrl: json["coverUrl"],
        count: json["count"],
        likeCount: json["likeCount"],
        isRecommend: json["isRecommend"],
        status: json["status"],
        isCommentOpen: json["isCommentOpen"],
        isTop: json["isTop"],
        publishTime: json["publishTime"],
        createTime: DateTime.parse(json["createTime"]),
        updateTime: DateTime.parse(json["updateTime"]),
        category: Category.fromJson(json["category"]),
        tags: List<Category>.from(json["tags"].map((x) => Category.fromJson(x))),
        author: Author.fromJson(json["author"]),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "summary": summary,
        "coverUrl": coverUrl,
        "count": count,
        "likeCount": likeCount,
        "isRecommend": isRecommend,
        "status": status,
        "isCommentOpen": isCommentOpen,
        "isTop": isTop,
        "publishTime": publishTime,
        "createTime": createTime.toIso8601String(),
        "updateTime": updateTime.toIso8601String(),
        "category": category.toJson(),
        "tags": List<dynamic>.from(tags.map((x) => x.toJson())),
        "author": author.toJson(),
    };
}

class Author {
    final String id;
    final String nickname;

    Author({
        required this.id,
        required this.nickname,
    });

    factory Author.fromRawJson(String str) => Author.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Author.fromJson(Map<String, dynamic> json) => Author(
        id: json["id"],
        nickname: json["nickname"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "nickname": nickname,
    };
}

class Category {
    final int id;
    final String name;

    Category({
        required this.id,
        required this.name,
    });

    factory Category.fromRawJson(String str) => Category.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Category.fromJson(Map<String, dynamic> json) => Category(
        id: json["id"],
        name: json["name"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
    };
}
